import java.util.*;

/**
 * @author LKQ
 * @date 2022/6/29 10:34
 * @description 使用HashMap存储映射，保持相同字符串加密之后的幂等性
 */
public class Solution {
    public static void main(String[] args) {

    }
    class Codec {
        Map<String, String> origin2Tiny = new HashMap<>(), tiny2Origin = new HashMap<>();
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        String prefix = "https://lkq.com/";
        int k = 6;
        Random random = new Random();

        // Encodes a URL to a shortened URL.将加密地址的随机六位加入到prefix后成密文
        public String encode(String longUrl) {
            while (!origin2Tiny.containsKey(longUrl)) {
                char[] cs = new char[k];
                for (int i = 0; i < k; i++) {
                    cs[i] = str.charAt(random.nextInt(str.length()));
                }
                String cur = prefix + String.valueOf(cs);
                if (tiny2Origin.containsKey(cur)) {
                    continue;
                }
                // 将密文->明文存储
                tiny2Origin.put(cur, longUrl);
                // 明文 -> 密文
                origin2Tiny.put(longUrl, cur);
            }
            return origin2Tiny.get(longUrl);
        }

        // Decodes a shortened URL to its original URL.
        public String decode(String shortUrl) {
            return tiny2Origin.get(shortUrl);
        }
    }
}
